import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../core/utils/show_toast.dart';
import '../../../../data/models/auth/user.dart';
import '../../../../data/controllers/player_controller.dart';

part 'public_players_state.dart';

class PublicPlayersCubit extends Cubit<PublicPlayersState> {
  PublicPlayersCubit() : super(PublicPlayersInitial());

  static PublicPlayersCubit get(context) => BlocProvider.of(context);

  List<User> users = [];

  void init() {
    getAllUsers();
  }

  Future getAllUsers()async{
    emit(PublicPlayersLoading());
    await PlayerController.getPublicPlayers().catchError((e){
      emit(PublicPlayersError());
      return null;
    }).then((value){
      if(value != null && value.state) {
        users = List<User>.generate(value.data.length, (index) => User.fromJson(value.data[index]));
        emit(PublicPlayersDone());
      }else if(value != null){
        showMessage(value.message);
        emit(PublicPlayersError());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
      }
    });
  }


}
